home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-07-26 | 5.6 KB | 215 lines | [TEXT/ttxt] |
- Stock Watch Project Script
-
- global name, theWindow, lastPrice, highPrice, lowPrice, change, successful, nextUpdate
-
- on run --the application has been launched without any particular document to open
- set theWindow to "Main"
-
- open window theWindow
- set the editable of column 2 of table "StockTable" of window theWindow to false
- set the editable of column 3 of table "StockTable" of window theWindow to false
- UpdateData()
-
-
- end run
-
- on idle
- local tix
- set tix to the ticks
-
- if tix > nextUpdate then
- UpdateData()
- else
-
- set the contents of textbox "Status" of window theWindow to ("Idle. Update in " & ((nextUpdate - tix) / 60) div 1 & " secs.")
- end if
-
- end idle
-
- on UpdateData()
- local theData, x
- set x to 1
- set symbol to ""
- set lastPrice to ""
- set highPrice to ""
- set lowPrice to ""
- set change to ""
- set successful to false
-
- repeat while x < row count of table "StockTable" of window theWindow
- copy the (contents of cell x of column 1 of table "StockTable" of window theWindow as text) to symbol
- if symbol = "" then
- set x to row count of table "StockTable" of window theWindow -- end now
- else
- GetQuote("yahoo", symbol)
- end if
-
- if x < row count of table "StockTable" of window theWindow then
- if successful then
- set the contents of cell x of column 2 of table "StockTable" of window theWindow to lastPrice as text
-
- -- if the change is downward, set pen color to red
- if (count of characters in change) > 0 then
- if character 1 of change = "-" then
- set pen color of (cell x of column 3 of table "StockTable" of window theWindow) to 30
- else
- set pen color of (cell x of column 3 of table "StockTable" of window theWindow) to black
- end if -- minus
- end if -- count
- set the contents of (cell x of column 3 of table "StockTable" of window theWindow) to change
- else
- set the contents of cell x of column 2 of table "StockTable" of window theWindow to "Error"
- set the contents of cell x of column 3 of table "StockTable" of window theWindow to "Error"
- end if --successful
- set x to x + 1
- end if -- x < row count
-
- end repeat
- set nextUpdate to (the ticks) + 7200
- end UpdateData
-
-
- on GetQuote(source, ticker)
-
- set the contents of textbox "Status" of window theWindow to "Retrieving \"" & ticker & "\" from " & source
-
- YahooQuote(ticker)
-
- if successful = false then
- set the contents of textbox "Status" of window theWindow to "Error"
- end if
-
- end GetQuote
-
- on YahooQuote(ticker)
- local theDoc
- local theData
- set successful to true
- tell application "WebMiner"
- set theDoc to open "http://quote.yahoo.com/q?s=" & ticker & "&d=v" without displaying
-
- -- here we will repeat until the document is completely downloaded and parsed
- repeat while not complete of theDoc
- end repeat
-
- set theData to the contents of cell 3 of table 4 of theDoc
-
- -- trim off leading ">", returns, tabs, and spaces
- set lastPrice to my trimfront(theData, ">", "")
- set lastPrice to my trimbothends(lastPrice, return, " ")
- set lastPrice to my trimbothends(lastPrice, return, tab)
- set lastPrice to my trimbothends(lastPrice, tab, " ")
- set lastPrice to my trimbothends(lastPrice, " ", "")
-
- set theData to the contents of cell 4 of table 4 of theDoc
-
- -- trim off leading ">", returns, tabs, and spaces
-
- set change to my trimfront(theData, ">", "")
- set change to my trimbothends(change, return, " ")
- set change to my trimbothends(change, return, tab)
-
- set change to my trimbothends(change, tab, " ")
- set change to my trimbothends(change, " ", "-")
- set change to my trimbothends(change, " ", "+")
-
- close theDoc
- end tell
-
- end YahooQuote
-
-
- on chosen theObj -- a menu item has been chosen
- copy name of window of theObj to theWindow
- copy name of theObj to theMenuItem
- copy title of menu of theObj to theMenu
-
- if index of menu of theObj = 1 then
- display dialog "Quote Monitor Copyright © 1998 Brookline Software. All rights reserved."
-
- else if theMenu is "File" then
- if theMenuItem = "Quit" then
- quit
- end if
- end if
-
- end chosen
-
- -- string trim, find and replace utility functions
-
- on trimbothends(str, trimchar, ignorechar)
- return trimback(trimfront(str, trimchar, ignorechar), trimchar, ignorechar)
- end trimbothends
-
- on trimfront(str, trimchar, ignorechar)
- local n, outstr
-
- set outstr to ""
-
- repeat with n from 1 to (count of characters in str)
- set ch to character n of str
-
- if ch is not trimchar then
- if ch is not ignorechar then
- set outstr to outstr & (characters n through (count of characters in str) of str) as string
- exit repeat
- else
- set outstr to outstr & ch
- end if
- end if
- end repeat
-
- return outstr
- end trimfront
-
-
- on trimback(str, trimchar, ignorechar)
- local n, outstr
-
- set outstr to ""
- repeat with n from (count of characters in str) to 1 by -1
- set ch to character n of str
-
- if ch is not trimchar then
- if ch is not ignorechar then
- set outstr to (characters 1 through (n - 1 - (count of characters in str)) of str & outstr) as string
- exit repeat
- else
- set outstr to ch & outstr
- end if
- end if
- end repeat
-
- return outstr
- end trimback
-
- on findreplace(inputstr, findstr, replacestr)
- local n, il, rl, fl, outstr
-
- set outstr to ""
-
- set il to count of characters in inputstr
- set fl to count of characters in findstr
- set rl to count of characters in replacestr
-
- set n to 1
-
- repeat
- if ((n + fl ≤ il) and (characters n through (n + fl - 1) of inputstr) as string = findstr) then
- set outstr to outstr & replacestr
- set n to n + fl
- else
- set outstr to outstr & (character n of inputstr)
- set n to n + 1
- end if
-
- if n is greater than il then
- exit repeat
- end if
-
- end repeat
-
- return outstr
-
- end findreplace
-